

import Secretary from '../frontend_services/secretary'
import UserData from '../UserData'
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        receiverPrefab: {
            type: cc.Prefab,
            default: null
        },
        senderPrefab: {
            type: cc.Prefab,
            default: null
        },
        openPhone: {
            type: cc.Animation,
            default: null
        },
        choose: cc.Node,
        yesText: cc.Label,
        noText: cc.Label,
        redDot: cc.Node,
        shakeAnimation: cc.Animation,
        scrollView: cc.ScrollView,
        tips: cc.Prefab,
    },
    shakePhone () {
        this.openPhone.play('shake')
    },
    slipIn () {
        let slipIn = cc.moveTo(0.6, cc.v2(0, 0)).easing(cc.easeExponentialOut())
        this.node.active = true
        this.node.runAction(slipIn)
    },
    slipOut () {
        let slipOut = cc.moveTo(0.6, cc.v2(-cc.winSize.width, 0)).easing(cc.easeExponentialOut())
        let deactive = cc.callFunc(() => {
           this.node.active = false 
        });

        this.node.runAction(cc.sequence(slipOut, deactive))
    },
    toggle () {
        if (!this.node.active) {
            this.slipIn()
        } else {
            this.slipOut()
        }
    },
    getContentNode () {
        let node = this.node.getChildByName('scrollview')
                .getChildByName('view')
                .getChildByName('content')
        return node
    },
    addTextPrefab (text, prefab) {
        let content = this.getContentNode()
        let p = cc.instantiate(prefab)
        p.getComponent('SecretaryPrefab').setText(text)
        content.addChild(p)
    },
    addReceiver (text) {
        this.addTextPrefab(text, this.receiverPrefab)
    },
    addSender (text) {
        this.addTextPrefab(text, this.senderPrefab)
    },
    nextQuestion () {
        if (this.question || this.question.nextQuestion) {
            this.question = this.question.nextQuestion
            this.addReceiver(this.question.question)
        }
    },
    updateQuestion (question) {
        this.question = question
        this.addReceiver(this.question.content)
        // this.setChildBtnText(this.question.yes.answer, 'yes')
        // this.setChildBtnText(this.question.no.answer, 'no')
        if(this.question.yes) {
            this.yesText.string = this.question.yes.answer
        }
        if (this.question.no) {
            this.noText.string = this.question.no.answer
        }
        this.scrollView.scrollToBottom()
    },
    questionAnswered() {
        this.choose.active = false
        this.redDot.active = false
        this.shakeAnimation.stop()
        setTimeout(() => {
            this.requestSecretaryData()
        }, (30 + (Math.floor(Math.random() * 10))) * 1000)
    },
    affect (effect) {
        let tips = cc.instantiate(this.tips)
        effect.money = effect.money * UserData.runtimeState.totalGPS *COMMON_VALUE.SECRETARY_GAIN_RATIO
        if (effect.money < 0) {
            effect.money = 0
        }
        effect.reputation = effect.reputation ? (UserData.getReputation() * 0.005) : 0
        if (effect.reputation < 0) {
            effect.reputation = 0
        }
        tips.getChildByName('money').getComponent('labelFixedRichText').setNumber(effect.money)
        tips.getChildByName('reputation').getComponent('labelFixedRichText').setNumber(effect.reputation)
        UserData.addCurrentMoney(effect.money)
        UserData.addReputation(effect.reputation)
        this.node.addChild(tips)
        tips.runAction(cc.spawn(
                cc.fadeOut(2).easing(cc.easeExponentialIn())
                ,cc.moveBy(2,cc.v2(0,Math.random() * 100 + 100)).easing(cc.easeExponentialOut()))
                ,cc.callFunc((tips) => {
                    tips.destroy()
                }))
    },
    yes () {
        if (!this.question.isAnswered) {
            this.addSender(this.question.yes.answer)
            this.question.isAnswered = true
            this.scrollView.scrollToBottom()
            this.affect(this.question.yes.effect)
        }

        if (this.question.yes.nextQuestion) {
            this.updateQuestion(this.question.yes.nextQuestion)
        } else {
            this.questionAnswered()
        }
    },
    no () {
        if (!this.question.isAnswered) {
            this.addSender(this.question.no.answer)
            this.question.isAnswered = true
            this.scrollView.scrollToBottom()
            this.affect(this.question.no.effect)
        }
        if (this.question.no.nextQuestion) {
            this.updateQuestion(this.question.no.nextQuestion)
        } else {
            this.questionAnswered()
        }
    },
    setChildBtnText (text, btnNodenName) {
        let yes = this.node.getChildByName(btnNodenName)
        let nodeText = yes.getChildByName('text')
        let label = nodeText.getComponent(cc.Label)
        label.string = text
    },
    // LIFE-CYCLE CALLBACKS:
    requestSecretaryData () {
        Secretary.getRandomSecretaryData().then((res) => {
            this.choose.active = true
            this.updateQuestion(res.data.data)
            this.redDot.active = true
            this.shakeAnimation.play('shake')
        }).catch((err) => {
            console.log(err)
        })
    },
    onLoad () {
        setTimeout(() => {
            this.requestSecretaryData()
        }, (30 + (Math.floor(Math.random() * 10))) * 1000)

    },
    start () {
    },
    update (dt) {
    },
});
